//
//  FeedbackViewController.m
//  TestARC
//
//  Created by 吴 晓泉 on 13-2-18.
//
//

#import "FeedbackViewController.h"
#import "JSON/SBJson.h"
#import "MyExtensions.h"
#import "UserManager.h"

@interface FeedbackViewController ()

@end

@implementation FeedbackViewController
@synthesize textView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.textView becomeFirstResponder];
    
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                               initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(sendFeedback:)];
    self.navigationItem.rightBarButtonItem = button;
}

-(void)viewDidUnload
{
    [super viewDidUnload];
    self.textView = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        //[self.textView resignFirstResponder];
        [self sendText:self.textView.text];
        return NO;
    }
    return YES;
}
                               
-(IBAction)sendFeedback:(id)sender
{

   [self sendText:self.textView.text];
}

-(void)sendText:(NSString *)text
{
   // NSLog(@"text=%@",text);
    [self showWaitView:nil];
    NSString * str = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString * URL_LOGIN =[NSString stringWithFormat:@"http://www.yaoxia.com/ajaxServlet?style=20a0d605af285106f58ae7435792d202&title=iphone&context=%@&userid=0",str];
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIHTTPRequest requestWithURL:url];
    [request setCompletionBlock:^{
        
        [self hideWaitView];
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"服务器信息"
                                          message:@"已发送，谢谢您的反馈！"//[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            
        }
        else
        {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"反馈信息"
                                          message:@"发送信息成功，谢谢您的反馈！"
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
        }
        
        //[self.navigationController popToRootViewControllerAnimated:YES];
        
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"无法连接到网络"
                              message:nil
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        //[self.navigationController popToRootViewControllerAnimated:YES];
    }];
    [request startAsynchronous];
}

@end
